﻿using Hidistro.Entities.Comments;
using Hidistro.UI.Common.Controls;
using JSShop.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class DetailPages_ReviewList : AscxTemplatedWebControl
    {
        List<Hishop_ProductReviews> _data;
        Literal TotalScore;
        Literal AverageScore;
        Literal AverageProp;
        Literal AllNum;
        Literal HighNum;
        Literal CenterNum;
        Literal LowNum;
        Repeater RList;
        wnxd_Pager_3q Pager;
        const int Pager_size = 6;
        int pn = 1;
        int status = 0;
        float total = 0;
        int high = 0;
        int center = 0;
        int low = 0;
        protected override void AttachChildControls()
        {
            this.TotalScore = (Literal)this.FindControl("TotalScore");
            this.AverageScore = (Literal)this.FindControl("AverageScore");
            this.AverageProp = (Literal)this.FindControl("AverageProp");
            this.AllNum = (Literal)this.FindControl("AllNum");
            this.HighNum = (Literal)this.FindControl("HighNum");
            this.CenterNum = (Literal)this.FindControl("CenterNum");
            this.LowNum = (Literal)this.FindControl("LowNum");
            this.RList = (Repeater)this.FindControl("RList");
            this.Pager = (wnxd_Pager_3q)this.FindControl("wnxd_Pager");
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "/ascx/tags/Common_Comment/DetailPages_ReviewList_3q.ascx";
            }
            base.OnInit(e);
        }
        public List<Hishop_ProductReviews> DataSource
        {
            set
            {
                if (value == null) this._data = null;
                else
                {
                    this.total = 0;
                    this.high = 0;
                    this.center = 0;
                    this.low = 0;
                    this._data = new List<Hishop_ProductReviews>();
                    foreach (Hishop_ProductReviews item in value)
                    {
                        if (item.Evaluate == null) item.Evaluate = (int)ProductReviewStatus.好评;
                        if (item.Score == null) item.Score = 5;
                        this._data.Add(item);
                        this.total += item.Score.Value;
                        switch ((ProductReviewStatus)item.Evaluate.Value)
                        {
                            case ProductReviewStatus.好评:
                                this.high++;
                                break;
                            case ProductReviewStatus.中评:
                                this.center++;
                                break;
                            case ProductReviewStatus.差评:
                                this.low++;
                                break;
                        }
                    }
                }
            }
        }
        private void ControlDataBind()
        {
            if (this._data != null && this._data.Count > 0)
            {
                this.TotalScore.Text = this.total.ToString("f0");
                float prop = this.total / this._data.Count;
                this.AverageScore.Text = prop.ToString("f1");
                this.AverageProp.Text = (prop * 20).ToString();
                this.AllNum.Text = this._data.Count.ToString();
                this.HighNum.Text = this.high.ToString();
                this.CenterNum.Text = this.center.ToString();
                this.LowNum.Text = this.low.ToString();
                if (this.status > (int)ProductReviewStatus.好评 && this.status <= (int)ProductReviewStatus.差评 + 1)
                {
                    this._data = this._data.Where(m => (int)m.Evaluate.Value == this.status - 1).OrderByDescending(m => m.ReviewDate).ToList();
                }
                if (this._data.Count > 0)
                {
                    this.RList.DataSource = this._data.Skip((this.pn - 1) * Pager_size).Take(Pager_size);
                    this.RList.DataBind();
                    if (this._data.Count > Pager_size)
                    {
                        this.Pager.Visible = true;
                        this.Pager.Index = this.pn;
                        this.Pager.Size = Pager_size;
                        this.Pager.Total = this._data.Count;
                    }
                }
                HtmlGenericControl li = (HtmlGenericControl)this.FindControl("Status_" + this.status.ToString());
                if (li != null)
                {
                    li.Attributes.Add("class", "select");
                }
            }
        }
        public override void DataBind()
        {
            this.EnsureChildControls();
            this.ControlDataBind();
        }
        public string ReviewListPager()
        {
            if (int.TryParse(this.Page.Request.Form["pn"], out this.pn))
            {
                if (this.pn > 0 && this.pn <= Math.Ceiling((decimal)this._data.Count / Pager_size))
                {
                    int.TryParse(this.Page.Request.Form["status"], out this.status);
                    this.AttachChildControls();
                    this.ControlDataBind();
                    this.Pager.EnsureChildControls();
                    using (StringWriter sw = new StringWriter())
                    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                    {
                        this.Render(htw);
                        return sw.ToString();
                    }
                }
            }
            return null;
        }
    }
}
